<?php

/**
    resizing function
    returns $thumb ($width x $height image)
*/
function getThumbByFilename($filename, $width=100, $height=100)
{
	$info =  getimagesize($filename);
	//print_r($info);
    $ext = $info[2];
	$img = null;
	switch($ext)
	{
		case IMAGETYPE_PNG: $img = imagecreatefrompng($filename); break;
		case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($filename); break;
		case IMAGETYPE_GIF: $img = imagecreatefromgif($filename); break;
		default: die("{$ext}, LOLWUT?");
	}
	$thumb = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumb, $img, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
	return $thumb;
}

/**
  returns numbers of 
  $histo - array int[256] + $histo['hash'] have bytehash of all file
*/
function getHistoByFilename($filename, $width=100, $height=100)
{
	//$thumb = getThumbByFilename($filename, $width, $height);
	
	$histo = array();
	$rr = $gg = $bb = 0;
	/*
	for($i=0;$i<$width;$i++)
	{
		//$lightness[$i] = array();
		for($j=0;$j<$height;$j++)
		{
			$rgb = ImageColorAt($thumb, $i, $j); 
			$rr = ($rgb >> 16) & 0xFF;
	        $gg = ($rgb >> 8) & 0xFF;
	        $bb = $rgb & 0xFF;
	        //if(!isset($histo[$gg]))$histo[$gg] = 0;
	        $histo[round(0.3 * $rr + 0.59 * $gg + 0.11 * $bb)]++;
		}
	}*/
	$histo['hash'] = md5_file($filename);
	return $histo;
}


/**
    return distance
   if $histo1['hash'] == $histo2['hash'], then return 0
       else 
*/
function getDistance($histo1, $histo2)
{
	if($histo1['hash'] == $histo2['hash']) return 0;
	
	$distance = 1;
	for($i=0;$i<256;$i++)
	{
		$distance += abs($histo2[$i] - $histo1[$i]);
		//echo 
	}
	return $distance;
}

function timeMeasure()
{
    list($msec, $sec) = explode(chr(32), microtime());
    return ($sec+$msec);
}

function scanDirForDub($dir, $threshold = 3000)
{
	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			$histos = array();
			while (($file = readdir($dh)) !== false) {
				if(is_file($dir . $file))
					if(getimagesize($dir . $file))
					{
						foreach($histos as $key => $histo)
						{
							$h2 = getHistoByFilename($dir . $file);
							$distance = getDistance($histo, $h2);
							if($distance>$threshold)
								$histos[$dir . $file] = $h2;
							else
								echo "{$key} is double of ".$dir . $file." - distance is {$distance}<br />";
						}
						if(!$histos)$histos[$dir . $file] = getHistoByFilename($dir . $file);
					}
			}
			//print_r($histos);
			
			foreach($histos as $key => $histo)
			{
				$info = pathinfo($key);
				copy($key, "./test/".$info['basename']);
			}
			
			closedir($dh);
		}
	}
}

/*
define('TIMESTART', timeMeasure());//Определяем константу в которой будем хранить время старта

//getThumbByFilename("12713696216401.Png");

$histo1 = getHistoByFilename("12712667524779__.jpg");
$histo2 = getHistoByFilename("12712667524779__.JPG");
$dis = getDistance($histo1, $histo2);
echo $dis;
//for($i=0;$i<256;$i++)echo $i.":".$r[$i]."<br />";
*/



/*
//print_r(pathinfo("./samples/"));exit;
set_time_limit (10000);
scanDirForDub("./samples/", 0);


echo '<br />Страница сгенерировалась за '.round(timeMeasure()-TIMESTART, 6).' сек.';

*/



